/*
 * @Author: dingxiao
 * @Date: 2025-06-18 16:28:24
 * @Description: 调研工作计划结果实体类
 */
import { timeConvert } from 'jdf-util';
import dayjs from 'dayjs';
import { isLeader } from '@/utils/utils';
import { COMPANY_LEADER_IDENTITY_CODES } from '@/utils/constants';
import DefaultResult from '../../common/DefaultResult';
import ResearchFormService from '../../ResearchFormService';

class ResearchWorkPlanResult extends DefaultResult {
  constructor(data, param) {
    super(param);
    this.data = data;
    this.param = param;
  }

  /**
   * 获取领导计划列表
   * @returns
   */
  getLeaderPlanList = async () => {
    const { userId } = this.param.processInstance.draftUser;
    const identityDefCode = sessionStorage.getItem('identityDefCode');
    const leaderPlanList = await new ResearchFormService().getLeaderPlanList({
      year: dayjs().year(),
      userId: isLeader(COMPANY_LEADER_IDENTITY_CODES, identityDefCode) ? userId : '',
    });
    return leaderPlanList ?? [];
  };

  /**
   * 获取表单返回值结果
   * @returns 表单保存的返回值
   */
  getData = async () => {
    const { data, param } = this;
    const {
      userName, userId, startTime, processInstance: { processInstanceId },
    } = param;
    const params = data ?? this.getDraftParams(param, {
      researchLedgers: await this.getLeaderPlanList(),
      createUser: userId,
      initiatorName: userName,
      statisticsDate: timeConvert(startTime),
      title: `公司领导调研工作开展情况-${dayjs().format('YYYYMMDD')}`,
    });
    const {
      // 业务表单自定义数据
      documentInfo,
    } = params;
    const document = JSON.parse(documentInfo);
    const workSubjects = await new ResearchFormService().allLeaderPlan({ id: processInstanceId }) || [];
    return {
      ...params,
      ...document,
      workSubjects,
    };
  }
}

export default ResearchWorkPlanResult;
